package io.polly.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;


/**
 * @author ldz
 * @version V1.0
 * @title: DataSourceConfig
 * @description: TODO(用一句话描述该文件做什么)
 * @date: 2022/4/6 16:07
 */
@Configuration
@ConditionalOnProperty(name = "spring.shardingsphere.datasource.type", havingValue = "com.zaxxer.hikari.HikariDataSource",
        matchIfMissing = true)
public class ShardingDataSourceConfig {

    @Resource
    private DataSource dataSource;

    @Primary
    @Bean("namedParameterJdbcTemplate")
    public NamedParameterJdbcTemplate jdbcTemplate() throws SQLException {
        return new NamedParameterJdbcTemplate(dataSource);
    }


}
